package org.jackysoft.struts2.workflow;

import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.User;
import org.jackysoft.opensymphony.workflow.entity.ActionDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowName;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.util.SpringSecurityUtils;
import org.jackysoft.util.WorkflowConstant;
import org.jackysoft.workflow.WorkItem;
import org.joda.time.DateTime;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


/**
 * <dd>根据工作流名称,初始化一个工作流 工作流名称 从</dd> 
 * <dd>上一个页面传递过来 实例变量 </dd> 
 * <dd>这个类只 初始化一个工作流实例然后把实例的</dd> 
 * <dd>所代表的工作流id 放到session中 去就返回</dd>
 * 
 * @author 屈甲康 
 * {@link}qujiakang@gmail.com
 * 
 */
@Controller
@Namespace("/workflow")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@InterceptorRefs({
	@InterceptorRef("defaultStack"),
    @InterceptorRef("annotationWorkflow")
})
public class StartWorkAction extends AbstractWorkflowAction<WorkItem> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8921155398778060682L;
	private WorkflowName bean;
	private Collection<WorkflowName> workflowDefines;
	public String init() throws Exception {
		User user = SpringSecurityUtils.getCurrentUser();
		log.info("用户 : " + user.getUsername()+"  启动工作流  "+ bean.getWorkflowName());
		if (null != bean.getWorkflowName()) {
			final WorkflowName workn = nameService.getByWorkflowName(bean);
			final WorkflowDescriptor desc = workn.getWorkflowDescriptor();
			final ActionDescriptor initAction = desc.getInitialAction(0);
			final int initiActionId = initAction.getId();
			workflowId = workflow.initialize(bean.getWorkflowName(), initiActionId, Collections.emptyMap());
			log.info("初始化工作流完成 :"+ workflowId);
			
			//log.info(desc.asXML());
			
			variableService.setVariable(workflowId,
					WorkflowConstant.FORM_TITLE.toString(), search);
			variableService.setVariable(workflowId,
					WorkflowConstant.ACTION_ID.toString(), "0");
			variableService.setVariable(workflowId,
					WorkflowConstant.ACTION_NAME.toString(), initAction == null ? "": initAction.getName());
			variableService.setVariable(workflowId,
					WorkflowConstant.STARTER.toString(), user.getLocalName());
			variableService.setVariable(workflowId,
					WorkflowConstant.STARTER_USERID.toString(), user.getUsername());
			
			variableService.setVariable(workflowId,
					WorkflowConstant.WORKFLOW_TITLE.toString(), search);
			variableService.setVariable(workflowId,
					WorkflowConstant.START_DATE.toString(), DateTime.now());
			variableService.setVariable(workflowId,
					WorkflowConstant.SEND_DATE.toString(), DateTime.now());
			variableService.setVariable(workflowId,
					WorkflowConstant.LAST_EXECUTOR.toString(), user
							.getLocalName());
			variableService.setVariable(workflowId,	WorkflowConstant.LAST_EXECUTOR_USERID.toString(), user.getUsername());
			WorkflowService.LOCAL_STATUS.set(WorkflowConstant.STATE_UNDERWAY);
			return "init";
		} else {
			return ERROR;
		}

	}

	
	
	@Override
	public String start() {
		workflowDefines = nameService.getSimpleifyWorkflows();
		return SUCCESS;
	}	

    public WorkflowName getBean() {
		return bean;
	}
	public void setBean(WorkflowName bean) {
		this.bean = bean;
	}



	public Collection<WorkflowName> getWorkflowDefines() {
		return workflowDefines;
	}



	public void setWorkflowDefines(Collection<WorkflowName> workflowDefines) {
		this.workflowDefines = workflowDefines;
	}

}